package com.huarui.saleReturn.button;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ewaf.framework.task.Engine;
import org.ewaf.framework.task.EngineAccess;
import org.ewaf.framework.task.Handler;

import com.huarui.button.EngineTypeEnum;
import com.huarui.saleReturn.model.SaleReturn;
import com.huarui.saleReturn.util.ConstantParamEnum;

public class SaleReturnButtonService{
	
	
	private EngineAccess engineAccess;
    /**
     * 产生不同状态下的按钮
     * @param status
     * @param currentorId
     * @param userId
     * @return ['isOwner':true/false,'buttons':xxxxx]
     */
	public Map<String,Object> generateButtonsByStatus(String status, Integer currentorId,Integer userId){
		//id，据此获得按钮
		String id = null;
		//判断单据所有者
		boolean isOwner = false;				
		//判断是否当前所有者打开单据
		if(currentorId != null && currentorId.equals(userId)){	
			isOwner = true;
		}
		if(status.equals(SaleReturn.STATUS_CREATE)){
			if(isOwner){
				id = SaleReturnButtonTypeEnum.STATUS_OF_CREATEBYOWNER.getType();
				
			}else{
				id = SaleReturnButtonTypeEnum.STATUS_OF_CREATEBYOTHER.getType();
			}
		}else if(status.equals(SaleReturn.STATUS_SUBMIT)){
			if(isOwner){
				id = SaleReturnButtonTypeEnum.STATUS_OF_SUBMITBYOWNER.getType();
			}else{
				id = SaleReturnButtonTypeEnum.STATUS_OF_SUBMITBYOTHER.getType();
			}
		}else if(status.equals(SaleReturn.STATUS_DELIVERY)){
			id = SaleReturnButtonTypeEnum.STATUS_OF_DELIVERY.getType();
		}else if(status.equals(SaleReturn.STATUS_NEW)){
			id = SaleReturnButtonTypeEnum.STATUS_OF_NEWCREATE.getType();
		}
		//按钮列表
		List<Handler> handlers = getButtons(id);
		Map<String,Object> buttons= new HashMap<String, Object>();
		buttons.put(ConstantParamEnum.ISOWNER.getType(), isOwner);
		buttons.put(ConstantParamEnum.BUTTONS.getType(), handlers);
		return buttons;
	}

	private List<Handler> getButtons(String id) {
		Engine engine = engineAccess.getEngine(EngineTypeEnum.ENGINE_TYPE_SALE_RETURN.getType());
	    List<Handler> handlers=engine.getHandlers(id);
	    return handlers;
	}

	public EngineAccess getEngineAccess() {
		return engineAccess;
	}

	public void setEngineAccess(EngineAccess engineAccess) {
		this.engineAccess = engineAccess;
	}
	
	
	

	
}
